package com.starblink.store.recommend.adapter

import android.app.Activity
import androidx.recyclerview.widget.GridLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.aroute.CommonRoute
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.dp2px
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.basic.util.ScreenUtils
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.rocketreserver.fragment.ProductF
import com.starblink.store.databinding.ItemRecommendProductBinding
import com.starblink.store.databinding.ItemRecommendStoreBinding
import kotlinx.coroutines.MainScope

class RecommendStoreHolder(
    binding: ItemRecommendStoreBinding,
    val activity: Activity,
    val pageRef: Int,
    val pageTrackTag: String,
) :
    BaseBindingVH<MerchantWebVOF, ItemRecommendStoreBinding>(binding = binding) {

    override fun onBind(merchat: MerchantWebVOF, position: Int) {
        binding.title.text = merchat.merchantName
        binding.ivStore.load(merchat.logo, merchat.merchantName)
        merchat.cornerLabels?.let {
            binding.ivSale.visibleOrGone(it.isNotEmpty() && it.first().type == 1) {

            }
        }
        setFollowState(merchat.subOrNot)

        itemView.click {
            CommonRoute.startStoreDetail(activity, merchat.id)
        }
        itemView.trackData(
            page = pageRef,
            element = SpmElementDef.ELE_33032,
            params = mapOf("merchantName" to "${merchat.merchantName}"),
            businessSuffixID = businessSuffixId(merchat)
        )

        merchat.productList?.let {
            binding.rvProduct.visibleOrGone(it.isNotEmpty()) {
                var products: MutableList<ProductF> = mutableListOf()

                merchat.productList?.forEach { it ->
                    products.add(it.productF)
                }
                val itemW = (ScreenUtils.getScreenWidth(context) - dp2px(118f)) / 4

                val layoutParams = binding.rvProduct.layoutParams
                layoutParams.height = itemW
                this.layoutParams = layoutParams

                this.layoutManager =
                    GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false)
                this.addItemDecoration(
                    SimpleGapItemDecor(horGap = 0, verGap = 0, withEdge = false)
                )
                val productAdapter: MutableAdapter by lazyOnNone {
                    MutableAdapter(products).apply {
                        addVhDelegate {
                            RecommendStoreProductHolder(
                                ItemRecommendProductBinding.inflate(it.inflater, it, false),
                                activity,
                                itemW, pageRef, merchat.merchantName, pageTrackTag
                            )
                        }
                    }
                }
                this.adapter = productAdapter
            }
        }

        binding.tvFollow.run {
            trackData(
                page = pageRef,
                element = SpmElementDef.GuideHomePageSubStoreClick,
                onlyHit = true,
                params = mapOf(
                    "merchantName" to "${merchat.merchantName}",
                    "status" to (if (UserDataCenter.isLogin()) "1" else "0"),
                    "result" to "${if (merchat?.subOrNot == true) "0" else "1"}"
                )
            )
            click {//关注
                CommonMutateManage.subscribeOrNotMerchant(
                    activity,
                    merchat.id,
                    merchat.merchantName ?: "", merchat.subOrNot, MainScope(),
                    {
//                        setFollowState(!merchat.subOrNot)
                    }, resultCallback = { state ->
                        if (state) {
                            val sub = !merchat.subOrNot
                            ApolloValSetExt.setApolloVal<MerchantWebVOF>(merchat, "subOrNot", sub)
                            setFollowState(sub)
                            trackData(
                                page = pageRef,
                                element = SpmElementDef.GuideHomePageSubStoreClick,
                                onlyHit = true,
                                params = mapOf(
                                    "merchantName" to "${merchat.merchantName}",
                                    "status" to (if (UserDataCenter.isLogin()) "1" else "0"),
                                    "result" to "${if (merchat?.subOrNot == true) "0" else "1"}"
                                )
                            )
                        }
                    }, merchant = merchat
                )
            }
//            if (merchat.merchantWebUrl?.isNotEmpty() == true) {
//                binding.tvWeb.text = Uri.parse(merchat.merchantWebUrl).host
//            }
            binding.tvFollowers.text =
                "${merchat.subNum}"
//            merchat.atmosphereLabels?.let {
//                binding.tvTag.visibleOrGone(it.isNotEmpty()) {
//                    binding.tvTag.text = merchat.atmosphereLabels!!.first().title
//                }
//            }
        }
    }


    private fun setFollowState(subOrNot: Boolean) {
        binding.tvFollow.visibleOrGone(!subOrNot)
//        if (subOrNot) {
//            binding.tvFollow.text =
//                CommUtils.getString(com.starblink.basic.style.R.string.following)
//            binding.tvFollow.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray300))
//            binding.tvFollow.setBackgroundResource(R.drawable.bg_btn_following)
//        } else {
//            binding.tvFollow.text =
//                CommUtils.getString(com.starblink.basic.style.R.string.follow)
//            binding.tvFollow.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.white))
//            binding.tvFollow.setBackgroundResource(R.drawable.bg_btn_follow)
//        }
    }
}
